import request from '@/utils/request'
import { otherFiltersItem } from '@/utils'
/**
 * 拆分图纸
 * @param {*} data
 * @returns
 */
export function putSplitPicture(data) {
  return request({
    url: '/api/v1/shopDesignPicture/splitPicture',
    method: 'PUT',
    data
  })
}
/**
 * 获取详情
 * @param {*} id
 * @returns
 */
export function getPictureInfo(id) {
  return request({
    url: `/api/v1/shopDesignPicture/getPictureInfo/${id}`,
    method: 'POST'
  })
}
/**
 * 图纸维护
 * @param {*} data
 * @returns
 */
export function postAddPictureMaintain(data) {
  return request({
    url: '/api/v1/designPictureMaintain/addPictureMaintain',
    method: 'POST',
    data
  })
}
/**
 * 获取维护记录
 * @param {*} picTaskCode
 * @returns
 */
export function getMaintains(picTaskCode) {
  return request({
    url: `/api/v1/designPictureMaintain/getMaintains/${picTaskCode}`,
    method: 'GET'
  })
}
/**
 * 图纸下发
 * @param {*} data
 * @returns
 */
export function drawingIssuance(data) {
  return request({
    url: '/api/v1/shopDesignPicture/drawingIssuance',
    method: 'POST',
    data
  })
}
/**
 * 发起异常
 * @param {*} data
 * @returns
 */
export function postShopException(data) {
  return request({
    url: '/api/v1/shopException',
    method: 'POST',
    data
  })
}
/**
 * 查询出图区域及明细
 * @param {*} data
 * @returns
 */
export function getDetailTaskAreaPage(data) {
  return request({
    url: '/api/v1/designOutpicDetailTask/getDetailTaskAreaPage',
    method: 'POST',
    data
  })
}
/**
 * 图纸管理详情获取出图明细
 * @param {*} data
 * @returns
 */
export function getDetailTaskAreaPageTwo(data) {
  return request({
    url: '/api/v1/designOutpicDetailTask/getDetailTaskAreaPageTwo',
    method: 'POST',
    data
  })
}
/**
 * 图纸审黑
 * @param {*} data
 * @returns
 */
export function putPictureApproval(data) {
  return request({
    url: '/api/v1/designPictureApproval/pictureApproval',
    method: 'PUT',
    data
  })
}
/**
 * 编辑图纸设计日志
 * @param {*} id
 * @param {*} designLog
 * @returns
 */
export function designLogEdit(id, designLog) {
  return request({
    url: '/api/v1/shopDesignPicture',
    method: 'PUT',
    data: {
      id,
      designLog
    }
  })
}

/**
 * 编辑图纸维护面积，重新计算时间
 * @param {*} data
 * @returns
 */
export function saveDrawing(data) {
  return request({
    url: '/api/v1/shopDesignTask/changeExpectEndTime',
    method: 'post',
    data
  })
}
/**
 * 图纸确定结果审核
 * @param {*} data
 * @returns
 */
export function postAddMaintainToExamine(data) {
  return request({
    url: '/api/v1/designPictureMaintain/addMaintainToExamine',
    method: 'POST',
    data
  })
}
/**
 * 获取出图明细详情
 * @param {*} id
 * @returns
 */
export function getBasicsInfo(id) {
  return request({
    url: `/api/v1/designOutpicDetailTask/getBasicsInfo/${id}`,
    method: 'GET'
  })
}
/**
 * 根据出图明细任务ID(detailTaskId)查询当前门店建设号下的出图区域
 * @param {*} detailTaskId
 * @returns
 */
export function postQueryAreaSeries(detailTaskId, filtersRaw = [], params = {}) {
  const data = new FormData()
  data.append('pageFlag', false)
  data.append('filtersRaw', JSON.stringify(filtersRaw))
  data.append('detailTaskId', detailTaskId)
  data.append('params', JSON.stringify(params))
  return request({
    url: '/api/v1/designOutpicDetailTask/queryAreaSeries/page',
    method: 'POST',
    data
  })
}
/**
 *设计区域及面积明细 保存
 * @param {*} data
 * @returns
 */
export function addOutpicAreaMaintain(data) {
  return request({
    url: '/api/v1/designOutpicAreaSeries/addOutpicAreaMaintain',
    method: 'POST',
    data
  })
}
/**
 * 设计区域及面积明细 编辑
 * @param {*} outpicDetailTaskCode
 * @returns
 */
export function postDetailTaskAreaPage(outpicDetailTaskCode, type) {
  const data = new FormData()
  data.append('pageFlag', false)
  const filtersRaw = []
  filtersRaw.push(otherFiltersItem('outpicDetailTaskCode', outpicDetailTaskCode, '='))
  data.append('filtersRaw', JSON.stringify(filtersRaw))
  if (type) {
    data.append('paramsRaw', JSON.stringify({ type }))
  }
  return request({
    url: '/api/v1/designOutpicDetailTask/getDetailTaskAreaPage',
    method: 'POST',
    data
  })
}
/**
 * 出图品类及数量维护
 * @param {*} data
 * @returns
 */
export function postCategoryInitialPage(outpicDetailTaskCode) {
  const data = new FormData()
  const filtersRaw = []
  filtersRaw.push(otherFiltersItem('outpicDetailTaskCode', outpicDetailTaskCode, '='))
  data.append('filtersRaw', JSON.stringify(filtersRaw))
  data.append('pageFlag', false)
  return request({
    url: '/api/v1/designOutpicDetailTask/getCategoryInitialPage',
    method: 'POST',
    data
  })
}
/**
 * 出图品类及数量维护
 * @param {*} data
 * @returns
 */
export function postCategoryNumberPage(outpicDetailTaskCode) {
  const data = new FormData()
  const filtersRaw = []
  filtersRaw.push(otherFiltersItem('outpicDetailTaskCode', outpicDetailTaskCode, '='))
  data.append('filtersRaw', JSON.stringify(filtersRaw))
  data.append('pageFlag', false)
  return request({
    url: '/api/v1/designOutpicDetailTask/getCategoryNumberPage',
    method: 'POST',
    data
  })
}
/**
 * 明细提交
 * @param {*} data
 * @returns
 */
export function postDetailCommit(data, isSave) {
  let url = `/api/v1/designOutpicDetailTask/detailCommit`
  if (isSave) {
    url = `/api/v1/designOutpicDetailTask/detailCommit?isSave=1`
  }
  return request({
    url,
    method: 'POST',
    data
  })
}
/**
 * 出图品类及数量新增
 * @param {*} data
 * @returns
 */
export function postAddCategoryNumber(data) {
  return request({
    url: '/api/v1/designOutpicCategoryNumber/addCategoryNumber',
    method: 'POST',
    data
  })
}
/**
 * 审核
 * @param {*} data
 * @returns
 */
export function postDetailToExamine(data) {
  return request({
    url: '/api/v1/designOutpicDetailTask/detailToExamine',
    method: 'POST',
    data
  })
}
/**
 * 施工图 区域机系列查询
 * @param {*} data
 * @returns
 */
export function postPageNew(detailTaskId, outpicDetailTaskCode) {
  const data = new FormData()
  const filtersRaw = []
  filtersRaw.push(otherFiltersItem('outpicDetailTaskCode', outpicDetailTaskCode, '='))
  data.append('filtersRaw', JSON.stringify(filtersRaw))
  data.append('pageFlag', false)
  data.append('paramsRaw', JSON.stringify({ detailTaskId, 'isProgramOutpic': 1 }))
  return request({
    url: '/api/v1/designOutpicDetailTask/queryAreaSeries/pageNew',
    method: 'POST',
    data
  })
}
/**
 * 图纸恢复
 * @param {*} data
 * @returns
 */
export function postDrawingRecover(data) {
  return request({
    url: '/api/v1/shopDesignPicture/drawingRecover',
    method: 'POST',
    data
  })
}
/**
 * 检查区域是否有方案
 * @param {*} belongRegions
 * @returns
 */
export function programAndSeriesList(belongRegions) {
  return request({
    url: `/api/v1/configProductProgram/programAndSeriesList?belongRegions=${belongRegions}`,
    method: 'GET'
  })
}

/**
 * 根据区域专区获取系列
 * @param {*} bigStyleCodes
 * @returns
 */
export function postConfigSeriesPage(bigStyleCodes) {
  const data = new FormData()
  data.append('pageFlag', false)
  const filtersRaw = [otherFiltersItem('bigStyleCode', `[${bigStyleCodes.join(',')}]`, 'in')]
  data.append('filtersRaw', JSON.stringify(filtersRaw))
  return request({
    url: `/api/v1/configSeries/page`,
    method: 'POST',
    data
  }).then(res => {
    return res
  })
}
/**
 * 根据主数据分类编码获取slm上级数据
 * @param {*} filtersRaw
 * @returns
 */
export const queryMainCategory = async(filtersRaw) => {
  const data = new FormData()
  data.append('pageFlag', false)
  data.append('filtersRaw', JSON.stringify(filtersRaw))
  const res = await request({
    url: `/select-service/api/v1/productCategory/queryCategoryByParamsPage`,
    method: 'POST',
    data
  })
  return [otherFiltersItem('categoryCode', `[${res.data.records.map(v => v.categoryCode).join(',')}]`, 'in'),
    otherFiltersItem('dr', `0`, '=')]
}
/**
 * 判断图纸是否出图完成
 * @param {*} outpicDetailTaskCode 出图任务号
 * @returns
 */
export function checkRepeatOutFinish(id) {
  return getBasicsInfo(id).then(resDetail => {
    const { sourcePic } = resDetail.data
    if (sourcePic === undefined || sourcePic === '') return
    const data = new FormData()
    data.append('pageFlag', false)
    const fr = []
    fr.push(otherFiltersItem('picTaskCode', sourcePic, '='))
    data.append('filtersRaw', JSON.stringify(fr))
    return request({
      url: '/api/v1/designOutpicDetailTask/getOutPicDetailTaskPage',
      method: 'POST',
      data,
      headers: {
        'Permission-App': 'slm'
      }
    }).then(res => {
      const records = res.data.records
      if (records && records.length === 1 && records[0].schedule === 'D') {
        return records[0]
      } else {
        return Promise.reject('当前为重复设计图纸，对应原图纸的出图明细尚未完成，是否继续？')
      }
    })
  })
}
/**
 * 查询空间
 * @param {*} areaCodes
 * @returns
 */
export function getSpace(areaCodes) {
  const data = new FormData()
  data.append('pageFlag', false)
  const filtersRaw = [
    otherFiltersItem('areaCode', `[${areaCodes.map(e => e.areaCode).join(',')}]`, 'in')
  ]
  data.append('filtersRaw', JSON.stringify(filtersRaw))
  return request({
    url: '/api/v1/configSpace/page',
    method: 'POST',
    data
  })
}
/**
 * 完成图纸
 * @param {*} data
 * @returns
 */
export function finishDrawing(data) {
  return request({
    url: `/api/v1/designPictureMaintain/pictureComplete`,
    method: 'post',
    data
  })
}
